package com.ruoyi.framework.web.codetable;

import java.sql.SQLException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"/codetablemanager"})
public class CodeTableManagerController {
    @Resource
    private CodeTableManagerService service;
    @Resource
    private CodetableService codetableService;

    public CodeTableManagerController() {
    }

    @RequestMapping(
        value = {"/gotoList"},
        method = {RequestMethod.GET}
    )
    public String gotoList(HttpServletRequest request) {
        return "/codetable/list";
    }

    @RequestMapping(
        value = {"/queryList"},
        method = {RequestMethod.POST}
    )
    @ResponseBody
    public String queryList(CodeTableManagerModel model, HttpServletRequest request) {
        return this.service.queryList(model);
    }

    @RequestMapping(
        value = {"/gotoAdd"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String gotoAdd(HttpServletRequest request) {
        return "/codetable/add";
    }

    @RequestMapping(
        value = {"/doAdd"},
        method = {RequestMethod.POST}
    )
    @ResponseBody
    public String doAdd(CodeTableManagerModel model) {
        CodeTableManagerModel model1 = this.service.getInfoById(model.getCodetable());
        if (model1 == null) {
            model.setOrderid("1");
            return this.service.add(model) ? "success" : "failure";
        } else {
            return "repeat";
        }
    }

    @RequestMapping(
        value = {"/gotoEdit"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        CodeTableManagerModel model = this.service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/codetable/edit");
        mav.addObject("model", model);
        return mav;
    }

    @RequestMapping(
        value = {"/doEdit"},
        method = {RequestMethod.POST}
    )
    @ResponseBody
    public String doEdit(CodeTableManagerModel model) {
        return this.service.edit(model) > 0 ? "success" : "failure";
    }

    @RequestMapping(
        value = {"/gotoDelete"},
        method = {RequestMethod.POST, RequestMethod.GET}
    )
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (this.service.delete(ids) > 0) {
            CodetableModel cm = new CodetableModel();
            cm.setCodetable(ids);
            this.codetableService.delete(cm);
            return "success";
        } else {
            return "failure";
        }
    }

    @RequestMapping(
        value = {"/gotoView"},
        method = {RequestMethod.GET}
    )
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        CodeTableManagerModel model = this.service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/codetable/view");
        mav.addObject("model", model);
        return mav;
    }

    @RequestMapping(
        value = {"/checkcodetable"},
        method = {RequestMethod.GET}
    )
    @ResponseBody
    public int checkcodetable(HttpServletRequest request) {
        String codetable = request.getParameter("codetable");

        try {
            int count = this.service.checkcodetable(codetable);
            return count;
        } catch (SQLException var4) {
            var4.printStackTrace();
            return 0;
        }
    }
}
